package com.uni.action;

import com.uni.entity.Page;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


/**
 * --------------project-------------
 *
 * @Author Unirithe  - Mr.Chao
 * - CSDN:
 * https://blog.csdn.net/Unirithe
 * - GitHub:
 * https://github.com/unirithe
 * @create 2021/12/22 12:52
 * ------------------------------------------
 */
@WebServlet(name = "PageAction", urlPatterns = "/shop")
public class PageAction extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String newPage = req.getParameter("page");
        HttpSession session = req.getSession();
        Page page = (Page) session.getAttribute("pageInfo");
        int nowPage = page.getPageNow();
        switch (newPage) {
            case "last":
                page.setPageNow(Integer.max(0, nowPage-1));
                break;
            case "next":
                page.setPageNow((nowPage + 1) % (page.getTotalPage() + 1));
                break;
            default:
                int i = Integer.parseInt(newPage) - 1;
                if(i >= 0 && i <= page.getTotalPage())
                    page.setPageNow(i);
                break;
        }
        session.setAttribute("pageInfo", page);
        req.getRequestDispatcher("/shop_center").forward(req, resp);
    }

}
